package com.fbee.smartcommunities.utils;

import com.sun.xml.internal.ws.api.pipe.ServerPipeAssemblerContext;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * ClassName: com.fbee.smartcommunities.utils
 * Datetime: 2024/8/21 下午3:23
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
public class StringUtil {

    public static ArrayList<String> stringToList(String s){
        ArrayList<String> list = new ArrayList<>();
        int index = 0;
        while(s.indexOf(',',index) != -1){
            // 截取 , 前的字符串
            String s1 = s.substring(index, s.indexOf(",",index));
            System.out.println(s1);
            list.add(s1);
            index = s.indexOf(",",index) + 1;
        }
        list.add(s.substring(index ));
        return list;
    }

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    // 生成订单号的方法
    public static String  generateOrderNumber(Integer openId,Integer goodId,Integer cartId){
        StringBuilder builder = new StringBuilder();
        // 获取当前时间戳（这里简化为字符串形式，实际可能需要转换为数字或特定格式）
        String timestamp = dateFormat.format(System.currentTimeMillis());
        builder.append(timestamp).append(String.format("%03d%03d%03d", openId, goodId, cartId));
        return builder.toString();
    }

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");

    // 生成商品编号的方法
    public static String generateCurrentDate() {
        return LocalDate.now().format(DATE_FORMATTER);
    }
    public static String generateUniqueId() {
        String datePart = generateCurrentDate();
        Random random = new Random();
        int randomNumber = random.nextInt(9999999);
        String randomPart = String.format("%07d", randomNumber);
        return datePart + randomPart;
    }

}
